package 代码记录.指定公司代码练习记录.腾讯云智.算法21年;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zx
 * @create 2023-03-13 19:13
 */
public class 螺旋矩阵_54 {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        int row = matrix.length;
        int col = matrix[0].length;
        int total = row * col;
        int count = 0;
        int left = 0,right = col - 1,up = 0,down = row - 1;
        while(count < total){
            for(int i = left;i <= right && count < total;i++){
                res.add(matrix[up][i]);
                count++;
            }
            up++;
            for(int i = up;i <= down && count < total;i++){
                res.add(matrix[i][right]);
                count++;
            }
            right--;
            for(int i = right;i >= left && count < total;i--){
                res.add(matrix[down][i]);
                count++;
            }
            down--;
            for(int i = down;i >= up && count < total;i--){
                res.add(matrix[i][left]);
                count++;
            }
            left++;
        }
        return res;
    }
}
